package fy.form.listen.operate;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.LogFactory;

import fy.form.listen.set.IFormSetting;
import fy.util.MyUtil;

public class FormOperatorCreator {

	private static org.apache.commons.logging.Log log = LogFactory
			.getLog(FormOperatorCreator.class);

	private static List<IFormSetting> formSettingList;

	// public static void setFormSettingList(List<IFormSetting> formSettingList)
	// {
	// FormOperatorCreator.formSettingList = formSettingList;
	// }
	//
	// public static List<IFormSetting> getFormSettingList() {
	// return formSettingList;
	// }

	// public static void addFormSetting(IFormSetting formSetting) {
	// if (formSettingList == null) {
	// formSettingList = new ArrayList<IFormSetting>();
	// }
	//
	// formSettingList.add(formSetting);
	// }

	private static void readFormListenSet() {
		try {

			BufferedReader br = MyUtil.createBufferedReader(MyUtil
					.getRealPath(IFormSetting.class) + "listen.conf");
			String temp = null;
			while (MyUtil.strIsNotNull((temp = br.readLine()))) {
				formSettingList.add((IFormSetting) Class.forName(temp)
						.newInstance());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static IFormOperator createOperator(String templateCode,
			short flowStatus) {
		IFormOperator operator = null;

		if (formSettingList == null) {
			synchronized (FormOperatorCreator.class) {
				if (formSettingList == null) {
					formSettingList = new ArrayList<IFormSetting>();
				}
				readFormListenSet();
			}
		}
		for (int i = 0, size = formSettingList.size(); i < size; i++) {
			IFormSetting fs = formSettingList.get(i);
			// 是否有针对该表单该事件的监听
			boolean eventMonitor = false;
			if (IFormSetting.START_STATUS == flowStatus) {
				eventMonitor = fs.isStartEventMonitor();
			} else if (IFormSetting.PROCESS_STATUS == flowStatus) {
				eventMonitor = fs.isProcessEventMonitor();
			} else if (IFormSetting.FINISH_STATUS == flowStatus) {
				eventMonitor = fs.isFinishEventMonitor();
			} else if (IFormSetting.TAKEBACK_STATUS == flowStatus) {
				eventMonitor = fs.isTakeBackEventMonitor();
			} else if (IFormSetting.CANCEL_STATUS == flowStatus) {
				eventMonitor = fs.isCancelEventMonitor();
			} else if (IFormSetting.STOP_STATUS == flowStatus) {
				eventMonitor = fs.isStopEventMonitor();
			} else if (IFormSetting.STEPBACK_STATUS == flowStatus) {
				eventMonitor = fs.isStepBackEventMonitor();
			}
//			log.info("456:"+flowStatus+","+fs.getClass());
//			log.info("123:"+eventMonitor+","+checkTemplateCode(fs.getTemplateCodes(), templateCode));
			if (eventMonitor
					&& checkTemplateCode(fs.getTemplateCodes(), templateCode)) {
				try {
					operator = (IFormOperator) Class.forName(
							fs.getOperatorClassName()).newInstance();
					operator.setFlowStatus(flowStatus);
				} catch (Exception e) {
					e.printStackTrace();
					log.error("表单数据处理发生异常，无法创建实例", e);
				}
				break;
			}
		}
		
		return operator;
	}

	
	private static boolean checkTemplateCode(String[] templateCodes,
			String templateCode) {
		for (int i = 0, length = templateCodes.length; i < length; i++) {
			if (templateCodes[i].equals(templateCode))
				return true;
		}
		return false;
	}
}
